package com.hhit.brpc.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baidu.brpc.exceptions.RpcException;
import com.baidu.brpc.interceptor.AbstractInterceptor;
import com.baidu.brpc.interceptor.InterceptorChain;
import com.baidu.brpc.protocol.Request;
import com.baidu.brpc.protocol.Response;

public class CustomInterceptor extends AbstractInterceptor {
	private static final Logger LOG = LoggerFactory.getLogger(CustomInterceptor.class);

	public boolean handleRequest(Request rpcRequest) {
		LOG.info("request intercepted, correlationId={}, service={}, method={}", rpcRequest.getCorrelationId(), rpcRequest.getTarget().getClass().getSimpleName(), rpcRequest.getTargetMethod().getName());
		return true;
	}

	@Override
	public void aroundProcess(Request request, Response response, InterceptorChain chain) throws RpcException {
		LOG.info("around intercepted, before proceed, correlationId={}, service={}, method={}", request.getCorrelationId(), request.getTarget().getClass().getSimpleName(), request.getTargetMethod().getName());

		chain.intercept(request, response);

		LOG.info("around intercepted, after proceed, correlationId={}, service={}, method={}", request.getCorrelationId(), request.getTarget().getClass().getSimpleName(), request.getTargetMethod().getName());
	}

	public void handleResponse(Response response) {
		if (response != null) {
			LOG.info("reponse intercepted, correlationId={}, result={}", response.getCorrelationId(), response.getResult());
		}
	}
}
